/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship_client.screens;

import battleship_client.Controller;
import java.awt.*;
import javax.swing.*;

/**
 *
 * @author jalejandrovc
 */
public class DataScreen extends Screen {

    public DataScreen(Controller wh, boolean newUsr) {
        windowHandler = wh;
        newUser = newUsr;
        setLookAndFeel();
        initComponents();
    }

    public DataScreen() {
        initComponents();
    }

    private void initComponents() {
        setTitle("Settings");
        setLocationRelativeTo(null);
        setResizable(false);
        setSize(400, 490);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        contentPane = getContentPane();
        mainPanel = new JPanel();

        mainPanel.setLayout(null);
        lblImage = new JLabel();
        lblWarning = new JLabel("Once you've chosen a username, it cannot be changed.");
        lblWarning.setForeground(Color.red);
        lblUserName = new JLabel("Username:");
        lblPassword = new JLabel("Password:");
        lblRepeatPassword = new JLabel("Repeat password:");
        lblName = new JLabel("Name:");
        lblEmail = new JLabel("E-mail:");
        txtUsername = new JTextField("");
        txtPassword = new JPasswordField("");
        txtRepeatPassword = new JPasswordField("");
        txtName = new JTextField("");
        txtEmail = new JTextField("");
        btnAccept = new JButton("Accept");
        btnCancel = new JButton("Cancel");
        lblErrorUsername = new JLabel("Error1");
        lblErrorUsername.setFont(new Font("Lucida Grande", 1, 11));
        lblErrorUsername.setForeground(Color.red);
        lblErrorPassword = new JLabel("Error2");
        lblErrorPassword.setFont(new Font("Lucida Grande", 1, 11));
        lblErrorPassword.setForeground(Color.red);
        lblErrorName = new JLabel("Error3");
        lblErrorName.setFont(new Font("Lucida Grande", 1, 11));
        lblErrorName.setForeground(Color.red);
        lblErrorEmail = new JLabel("Error4");
        lblErrorEmail.setFont(new Font("Lucida Grande", 1, 11));
        lblError = new JLabel ("Error5");
        lblError.setFont(new Font("Lucida Grande", 1, 11));
        lblError.setForeground(Color.red);
        lblErrorEmail.setForeground(Color.red);
        lblErrorUsername.setVisible(false);
        lblErrorName.setVisible(false);
        lblErrorPassword.setVisible(false);
        lblErrorEmail.setVisible(false);
        lblError.setVisible(false);

        lblImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/400x100.png")));
        lblImage.setBounds(0,0,400,100);
        lblWarning.setBounds(35, 120, 500, 15);
        lblUserName.setBounds(35, 150, 120, 15);
        txtUsername.setBounds(165, 150, 180, 30);
        lblErrorUsername.setBounds(167, 180, 200, 15);
        lblName.setBounds(35, 195, 120, 15);
        txtName.setBounds(165, 195, 180, 30);
        lblErrorName.setBounds(167, 225, 200, 15);
        lblPassword.setBounds(35, 240, 120, 15);
        txtPassword.setBounds(165, 240, 180, 30);
        lblRepeatPassword.setBounds(35, 285, 120, 15);
        txtRepeatPassword.setBounds(165, 285, 180, 30);
        lblErrorPassword.setBounds(167,315,200,15);
        lblEmail.setBounds(35, 330, 120, 15);
        txtEmail.setBounds(165, 330, 180, 30);
        lblErrorEmail.setBounds(167,360,200,15);
        lblError.setBounds(35, 375, 250, 15);
        btnCancel.setBounds(80, 410, 100, 30);
        btnCancel.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                windowHandler.cancel(newUser);
            }
        });
        btnAccept.setBounds(210, 410, 100, 30);
        final DataScreen dataScreen = this;
        btnAccept.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblErrorUsername.setVisible(false);
                lblErrorName.setVisible(false);
                lblErrorPassword.setVisible(false);
                lblErrorEmail.setVisible(false);
                lblError.setVisible(false);
                if (newUser) {
                    windowHandler.validator.validatePersonalData(txtName.getText(), txtUsername.getText(), txtPassword.getPassword(), txtRepeatPassword.getPassword(), txtEmail.getText(), true, dataScreen);
                } else {
                    windowHandler.validator.validatePersonalData(txtName.getText(), txtUsername.getText(), txtPassword.getPassword(), txtRepeatPassword.getPassword(), txtEmail.getText(), false, dataScreen);
                }
            }
        });

        mainPanel.add(lblImage);
        mainPanel.add(lblWarning);
        mainPanel.add(lblEmail);
        mainPanel.add(lblName);
        mainPanel.add(lblPassword);
        mainPanel.add(lblRepeatPassword);
        mainPanel.add(lblUserName);
        mainPanel.add(lblErrorUsername);
        mainPanel.add(lblErrorPassword);
        mainPanel.add(lblErrorName);
        mainPanel.add(lblErrorEmail);
        mainPanel.add(lblError);
        mainPanel.add(txtEmail);
        mainPanel.add(txtName);
        mainPanel.add(txtPassword);
        mainPanel.add(txtRepeatPassword);
        mainPanel.add(txtUsername);
        mainPanel.add(btnAccept);
        mainPanel.add(btnCancel);
        contentPane.add(mainPanel);
    }

    public void setTexts(String name, String username, String password, String email) {
        txtEmail.setText(email);
        txtName.setText(name);
        txtPassword.setText(password);
        txtRepeatPassword.setText(password);
        txtUsername.setText(username);
        txtUsername.setEnabled(false);
    }

    public void setLblErrorUsername(String message){
        lblErrorUsername.setText(message);
        lblErrorUsername.setVisible(true);
    }
    public void setLblErrorPassword(String message){
        lblErrorPassword.setText(message);
        lblErrorPassword.setVisible(true);
    }
    public void setLblErrorName(String message){
        lblErrorName.setText(message);
        lblErrorName.setVisible(true);
    }
    public void setLblErrorEmail(String message){
        lblErrorEmail.setText(message);
        lblErrorEmail.setVisible(true);
    }

    public void setLblError(String message){
        lblError.setText(message);
        lblError.setVisible(true);
    }

    private Controller windowHandler;
    private Container contentPane;
    private JPanel mainPanel;
    private JLabel lblImage;
    private JLabel lblWarning;
    private JLabel lblUserName;
    private JLabel lblPassword;
    private JLabel lblRepeatPassword;
    private JLabel lblName;
    private JLabel lblEmail;
    private JLabel lblErrorUsername;
    private JLabel lblErrorPassword;
    private JLabel lblErrorName;
    private JLabel lblErrorEmail;
    private JLabel lblError;
    private JTextField txtUsername;
    private JPasswordField txtPassword;
    private JPasswordField txtRepeatPassword;
    private JTextField txtEmail;
    private JTextField txtName;
    private JButton btnAccept;
    private JButton btnCancel;
    private boolean newUser;
}
